package com.szeldon.utils;

/**
 * Class with utility methods for working with values.
 * 
 * @author szeldon
 * 
 */
public final class ValuesUtils {

    /**
     * Checks if argument isn't null. If it is, IllegalArgumentException is thrown with "${name} can't be null" as
     * description. Otherwise this method just returns argument.
     * 
     * @param name
     *            Name of argument. To be used for creating meaningful description.
     * @param argument
     *            Argument to be checked for being null.
     * 
     */
    public static String checkNamedValueForNull(String name, String argument) {
        if (argument == null) {
            throw new IllegalArgumentException("\"" + name + "\" can't be null.");
        }

        return argument;
    }

    /**
     * Checks whether given object is in given array. If at least one array element equals ({@link #equals(Object)})
     * given object, true is returned. False otherwise.
     * 
     * @param object
     * @param array
     * @return
     */
    public static boolean checkIfObjectIsInArray(Object object, Object[] array) {
        for (Object currentObject : array) {
            if (object.equals(currentObject)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Returns true if given reference is null. False otherwise.
     * 
     * @param object
     * @return
     */
    public static boolean isNull(Object object) {
        if (object == null) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Converts given number into a string representation that is two-digits long. For example, passing "1" to this
     * method results with "01" string. Numbers bigger than 99 or smaller than 0 result in
     * {@link IllegalArgumentException}.
     * 
     * @param number
     * @return
     */
    public static String convertToTwoDigitString(int number) {
        String numberAsString = "" + number;
        if (number < 0 || number > 99) {
            throw new IllegalArgumentException("Number " + number + " can't be converted into two-digit string.");
        } else if (numberAsString.length() == 1) {
            return "0" + numberAsString;
        } else {
            return numberAsString;
        }
    }
}
